package com.twetec.cms.adapter.html.frondend.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置-redis缓存
 */
@Configuration
@EnableAutoConfiguration
public class RedisCacheConfig {

    @Value("${redis.database.zero}")
    private int zeroDatabase;

    @Value("${redis.host}")
    private String host;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.port}")
    private int port;

    @Value("${redis.timeout}")
    private int timeout;

    @Value("${redis.pool.max-idle}")
    private int maxIdle;

    @Value("${redis.pool.min-idle}")
    private int minIdle;

    @Value("${redis.pool.max-wait}")
    private int maxWait;

    /**
     * redis 集群配置项
     */
    @Value("${redis.cluster.nodes}")
    private String nodes;

    /**
     * redis 集群配置项
     */
    @Value("${redis.cluster.max-redirects}")
    private int maxRedirects;

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWait);

        return jedisPoolConfig;
    }

    @Bean(name = "zeroJedisConnectionFactory")
    public JedisConnectionFactory zeroJedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {

        // redis单个连接配置
//        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//        redisStandaloneConfiguration.setHostName(host);
//        redisStandaloneConfiguration.setPort(port);
//        redisStandaloneConfiguration.setDatabase(zeroDatabase);
//        redisStandaloneConfiguration.setPassword(password);

        // redis集群配置
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        String[] sub = nodes.split(",");
        List<RedisNode> nodeList = new ArrayList<>(sub.length);
        String[] tmp;
        for (String s : sub) {
            tmp = s.split(":");
            nodeList.add(new RedisNode(tmp[0], Integer.valueOf(tmp[1])));
        }
        redisClusterConfiguration.setClusterNodes(nodeList);
        redisClusterConfiguration.setMaxRedirects(maxRedirects);
        redisClusterConfiguration.setPassword(password);

        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolingClientConfigurationBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
        jedisPoolingClientConfigurationBuilder.poolConfig(jedisPoolConfig);

        return new JedisConnectionFactory(redisClusterConfiguration, jedisPoolingClientConfigurationBuilder.build());
    }


    @Bean(name = "zeroStringRedisTemplate")
    public StringRedisTemplate zeroStringRedisTemplate(JedisConnectionFactory zeroJedisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(zeroJedisConnectionFactory);

        return stringRedisTemplate;
    }

    @Bean(name = "zeroRedisTemplate")
    public RedisTemplate<String, Object> zeroRedisTemplate(JedisConnectionFactory zeroJedisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();

        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());

        redisTemplate.setConnectionFactory(zeroJedisConnectionFactory);

        return redisTemplate;
    }


//    @Bean(name = "oneJedisConnectionFactory")
//    public JedisConnectionFactory oneJedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
//        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//        redisStandaloneConfiguration.setHostName(host);
//        redisStandaloneConfiguration.setPort(port);
//        redisStandaloneConfiguration.setDatabase(1);
//
//        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolingClientConfigurationBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
//        jedisPoolingClientConfigurationBuilder.poolConfig(jedisPoolConfig);
//
//        return new JedisConnectionFactory(redisStandaloneConfiguration, jedisPoolingClientConfigurationBuilder.build());
//    }
//
//    @Bean(name = "oneStringRedisTemplate")
//    public StringRedisTemplate oneStringRedisTemplate(JedisConnectionFactory oneJedisConnectionFactory) {
//        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(oneJedisConnectionFactory);
//
//        return stringRedisTemplate;
//    }
//
//    @SuppressWarnings("all")
//    @Bean(name = "oneRedisTemplate")
//    public RedisTemplate<String, Object> oneRedisTemplate(JedisConnectionFactory oneJedisConnectionFactory) {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
//
//        // key 序列化
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//
//        redisTemplate.setKeySerializer(stringRedisSerializer);
//        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//
//        // value 序列化
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//
//        redisTemplate.afterPropertiesSet();
//
//        // 注入工厂
//        redisTemplate.setConnectionFactory(oneJedisConnectionFactory);
//
//        return redisTemplate;
//    }

}
